package ldkxingzhe.ldkxingzhedemo.tab;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by ke on 15-8-28.
 */
public class OffLineMapActivity extends FragmentActivity implements View.OnClickListener{

    private ViewPager mViewPager;
    private MyPagerAdapter mAdapter;
    private TextView leftText,rightText;

    public final Fragment[] fragments = new Fragment[]{
            new OfflineMapDownloadManagerFragment(),new CityListFragment()
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offline_download);
        mViewPager = (ViewPager)findViewById(R.id.view_pager);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(mAdapter);
        findViewById(R.id.title_btn_left).setOnClickListener(this);
        leftText = (TextView)findViewById(R.id.left_tag);
        leftText.setOnClickListener(this);
        rightText = (TextView)findViewById(R.id.right_tag);
        rightText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_btn_left:
                onBackPressed();
                break;
            case R.id.left_tag:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.right_tag:
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            Log.d("TAG","onPageSelected position = " + position);
            if(position == 0){
                leftText.setBackgroundResource(R.drawable.mine_offlinearrow_tab1_pressed);
                leftText.setTextColor(Color.WHITE);
                rightText.setBackgroundResource(R.drawable.mine_offlinearrow_tab2_normal);
                rightText.setTextColor(Color.BLUE);
            }else{
                leftText.setBackgroundResource(R.drawable.mine_offlinearrow_tab1_normal);
                leftText.setTextColor(Color.BLUE);
                rightText.setBackgroundResource(R.drawable.mine_offlinearrow_tab2_pressed);
                rightText.setTextColor(Color.WHITE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d("TAG","onPageScrollStateChanged, state " + state);
        }
    }
}
